﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.Common;
using Hxj.Tools.EntityDesign.Model;
using System.Xml;
using System.Windows.Forms;
using Dos.ORM;
using Dos.ORM.Common;
using Hxj.Tools.EntityDesign;

namespace Dos.Tools.Common
{
    public class GenMapper
    {
        private List<Hxj.Tools.EntityDesign.Model.ColumnInfo> _columns = new List<Hxj.Tools.EntityDesign.Model.ColumnInfo>();

        private string _tableName;
        private string _dbType;

        private string _nameSpace = "BLL.Model";

        private string _className;

        private bool _isView = false;

        private bool _isSZMDX = false;
        private bool _isNewModel = false;
        private bool _isAdmin = false;
        public string ViewName { get; set; }
        public GenMapper(string tableName, string nameSpace, string className, List<Hxj.Tools.EntityDesign.Model.ColumnInfo> columns, bool isView, bool isSZMDX, string dbType = null, bool isNewModel = false, bool isAdmin = false)
        {
            _isSZMDX = isSZMDX;
            _className = Utils.ReplaceSpace(className);
            _nameSpace = Utils.ReplaceSpace(nameSpace);
            _tableName = tableName;
            _dbType = dbType;
            _isNewModel = isNewModel;
            _isAdmin = isAdmin;
            if (_isSZMDX)
            {
                _className = Utils.ToUpperFirstword(_className);
                TableName = Utils.ToUpperFirstword(TableName);
                ViewName = Utils.ToUpperFirstword(ViewName);
            }
            _isView = isView;

            foreach (Hxj.Tools.EntityDesign.Model.ColumnInfo col in columns)
            {
                col.ColumnName = Utils.ReplaceSpace(col.ColumnName);
                if (_isSZMDX)
                {
                    col.ColumnName = Utils.ToUpperFirstword(col.ColumnName);
                }

                col.DeText = Utils.ReplaceSpace(col.DeText);
                _columns.Add(col);
            }
        }

        public List<Hxj.Tools.EntityDesign.Model.ColumnInfo> Columns
        {
            get { return _columns; }
            set { _columns = value; }
        }
        public string TableName
        {
            get { return _tableName; }
            set { _tableName = value; }
        }
        public string NameSpace
        {
            get { return _nameSpace; }
            set { _nameSpace = value; }
        }
        public string ClassName
        {
            get { return _className; }
            set { _className = value; }
        }
        public string DbType
        {
            get { return _dbType; }
            set { _dbType = value; }
        }
        public bool IsView
        {
            get { return _isView; }
            set { _isView = value; }
        }
        public bool GetByView { get; set; }

        public string Builder()
        {
            Columns = DbToCS.DbtoCSColumns(Columns, DbType, true);
            string tbname = TableName;
            if (GetByView)
            {
                tbname = ViewName;
            }
            if (_isSZMDX)
            {
                ViewName = Utils.ToUpperFirstword(ViewName);
            }
            StringPlus plus = new StringPlus();
            plus.AppendLine("//------------------------------------------------------------------------------");
            plus.AppendLine("// <auto-generated>");
            plus.AppendLine("//     此代码由工具生成。");
            plus.AppendLine("//     运行时版本:" + Environment.Version.ToString());
            plus.AppendLine("//     对此文件的更改可能会导致不正确的行为，并且如果");
            plus.AppendLine("//     重新生成代码，这些更改将会丢失。");
            plus.AppendLine("// </auto-generated>");
            plus.AppendLine("//------------------------------------------------------------------------------");
            plus.AppendLine();
            plus.AppendLine();
            plus.AppendLine("using System;");
            plus.AppendLine("using AutoMapper;");
            //plus.AppendLine("using DAL.Model;");
            //plus.AppendLine("using DAL.MDM.Model;");
            //plus.AppendLine("using Domain.MDM.Admin.Models;");
            plus.AppendLine("using " + NameSpace + ".DAL.Models;");
            plus.AppendLine("using " + NameSpace + (_isAdmin ? ".BLL.Admin.Models;" : ".BLL.Models;"));

            plus.AppendLine();


            plus.AppendLine();
            plus.AppendLine("namespace " + NameSpace + (_isAdmin ? ".BLL.Admin.Common.MapperConfig" : ".BLL.Common.MapperConfig"));
            plus.AppendLine("{");
            plus.AppendLine();
            //plus.AppendSpaceLine(1, "public  class " + ClassName + " : Profile");
            plus.AppendSpaceLine(1, "public  class " + ClassName);
            plus.AppendSpaceLine(1, "{");

            //内容
            //plus.AppendSpaceLine(2, "protected override void Configure()");
            plus.AppendSpaceLine(2, "public static void Init()");
            plus.AppendSpaceLine(2, "{");
            //plus.AppendSpaceLine(3, "CreateMap<" + TableName + "_Req, " + TableName + "_Condition>();");
            //plus.AppendSpaceLine(3, "CreateMap<" + tbname + ", " + TableName + "_Res>();");

            //plus.AppendSpaceLine(3, "CreateMap<" + TableName + "_Req, " + TableName + ">();");

            plus.AppendSpaceLine(3, "Mapper.CreateMap<" + TableName + "_Req, " + TableName + "_Condition>();");
            plus.AppendSpaceLine(3, "Mapper.CreateMap<" + tbname + ", " + TableName + "_Res>();");

            plus.AppendSpaceLine(3, "Mapper.CreateMap<" + TableName + "_Req, " + TableName + ">();");

            plus.AppendSpaceLine(2, "}");

            plus.AppendLine();


            plus.AppendSpaceLine(1, "}");
            plus.AppendLine("}");
            plus.AppendLine("");
            return plus.ToString();
        }


    }
}
